
//
//  MyMainTabbarVC.m
//  MyNewProject
//
//  Created by sun on 2018/12/2.
//  Copyright © 2018 sun. All rights reserved.
//

#import "MyMainTabbarVC.h"
#import "BaseViewController.h"
#import "MainPageController.h"
#import "MallViewController.h"
#import "HistoryViewController.h"
#import "PersonalCenterViewController.h"

#pragma mark -- mark 2.0

#import "CalssifyViewController.h"
#import "AuctionViewController.h"
#import "AuctionOrderListVC.h"
#import "MinesViewController.h"

static CGFloat const SKTabBarControllerHeight = 40.f;

@implementation SKNavigationController

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    } else {
        viewController.hidesBottomBarWhenPushed = NO;
    }
    [super pushViewController:viewController animated:animated];
}


@end


@implementation MyMainTabbarVC

- (instancetype)init {
    if (!(self = [super init])) {
        return nil;
    }
    /**
     * 以下两行代码目的在于手动设置让TabBarItem只显示图标，不显示文字，并让图标垂直居中。
     * 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
     * 更推荐后一种做法。
     */
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
    UIOffset titlePositionAdjustment = UIOffsetMake(0, 0);
    CYLTabBarController *tabBarController = [CYLTabBarController tabBarControllerWithViewControllers:self.viewControllers
                                                                               tabBarItemsAttributes:self.tabBarItemsAttributesForController
                                                                                         imageInsets:imageInsets
                                                                             titlePositionAdjustment:titlePositionAdjustment
                                                                                             context:nil
                                             ];
    [self customizeTabBarAppearance:tabBarController];
    return (self = (MyMainTabbarVC *)tabBarController);
}

- (NSArray *)viewControllers {
    //首页
//    MainPageController * first = [MainPageController new];
    CalssifyViewController *first = [CalssifyViewController new];
    first.gk_backStyle = GKNavigationBarBackStyleNone;
    SKNavigationController *firstNavigationController = [[SKNavigationController alloc]
                                                   initWithRootViewController:first];
    
    //圈子
//    MallViewController * second = [MallViewController new];
    AuctionViewController *second = [[AuctionViewController alloc] init];
    second.gk_backStyle = GKNavigationBarBackStyleNone;
    SKNavigationController *secondNavigationController = [[SKNavigationController alloc]
                                                    initWithRootViewController:second];
    
    //超级大牌
//    HistoryViewController * third = [HistoryViewController new];
//    third.gk_backStyle = GKNavigationBarBackStyleNone;
//    SKNavigationController *thirdNavigationController = [[SKNavigationController alloc]
//                                                   initWithRootViewController:third];
    AuctionOrderListVC *vc3 = [[AuctionOrderListVC alloc] init];
    vc3.gk_backStyle = GKNavigationBarBackStyleNone;
    vc3.gk_navTitle = @"成交记录";
    SKNavigationController *na3 = [[SKNavigationController alloc] initWithRootViewController:vc3];
    
    //我的
//    PersonalCenterViewController * four = [PersonalCenterViewController new];
    MinesViewController *four = [[MinesViewController alloc] init];
    four.gk_backStyle = GKNavigationBarBackStyleNone;
    SKNavigationController *fourthNavigationController = [[SKNavigationController alloc]
                                                    initWithRootViewController:four];
    NSArray *viewControllers = @[
                                 firstNavigationController,
                                 secondNavigationController,
                                 na3,
                                 fourthNavigationController
                                 ];
    return viewControllers;
}

- (NSArray *)tabBarItemsAttributesForController {
    
    NSDictionary *shopAttributes    = @{
                                        CYLTabBarItemTitle : @"商城",
                                        CYLTabBarItemImage : @"shoping_default",
                                        CYLTabBarItemSelectedImage : @"shoping_selected",
                                        CYLTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
                                        };
    NSDictionary *auctionAttributes = @{
                                        CYLTabBarItemTitle : @"拍场",
                                        CYLTabBarItemImage : @"auction_default",
                                        CYLTabBarItemSelectedImage : @"auction_selected",
                                        CYLTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
                                        };
    NSDictionary *attributes3    = @{
                                        CYLTabBarItemTitle : @"历史",
                                        CYLTabBarItemImage : @"log_default",
                                        CYLTabBarItemSelectedImage : @"log_selected",
                                        CYLTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
                                        };
    NSDictionary *mineAttributes    = @{
                                        CYLTabBarItemTitle : @"我的",
                                        CYLTabBarItemImage : @"mine_default",
                                        CYLTabBarItemSelectedImage : @"mine_selected",
                                        CYLTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
                                        };
    return @[shopAttributes, auctionAttributes, attributes3, mineAttributes];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
    [self becomeFirstResponder];
    self.delegate =self;
}

/**
 *  更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
 */
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarController {
    // 设置文字属性
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Semibold" size: 10],NSForegroundColorAttributeName:SKColorWithHex(0x999999)} forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Semibold" size: 10],NSForegroundColorAttributeName:SKColorWithHex(0x333333)} forState:UIControlStateSelected];
    
    // 如果你的App需要支持横竖屏，请使用该方法移除注释 '//'
    [self updateTabBarCustomizationWhenTabBarItemWidthDidUpdate];
}

- (void)updateTabBarCustomizationWhenTabBarItemWidthDidUpdate {
    void (^deviceOrientationDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        if ((orientation == UIDeviceOrientationLandscapeLeft) || (orientation == UIDeviceOrientationLandscapeRight)) {
            NSLog(@"Landscape Left or Right !");
        } else if (orientation == UIDeviceOrientationPortrait) {
            NSLog(@"Landscape portrait!");
        }
        [self customizeTabBarSelectionIndicatorImage];
    };
    [[NSNotificationCenter defaultCenter] addObserverForName:CYLTabBarItemWidthDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:deviceOrientationDidChangeBlock];
}

- (void)customizeTabBarSelectionIndicatorImage {
    ///Get initialized TabBar Height if exists, otherwise get Default TabBar Height.
    CGFloat tabBarHeight = SKTabBarControllerHeight;
    CGSize selectionIndicatorImageSize = CGSizeMake(CYLTabBarItemWidth, tabBarHeight);
    //Get initialized TabBar if exists.
    UITabBar *tabBar = [self cyl_tabBarController].tabBar ?: [UITabBar appearance];
    [tabBar setSelectionIndicatorImage:
     [[self class] imageWithColor:[UIColor whiteColor]
                             size:selectionIndicatorImageSize]];
}

+ (UIImage *)scaleImage:(UIImage *)image {
    CGFloat halfWidth = image.size.width/2;
    CGFloat halfHeight = image.size.height/2;
    UIImage *secondStrechImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(halfHeight, halfWidth, halfHeight, halfWidth) resizingMode:UIImageResizingModeStretch];
    return secondStrechImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
